package com.pureut.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.production.domain.LineStock;
import com.pureut.production.domain.dto.LineStockDto;
import com.pureut.production.domain.dto.LineStockSummaryDto;
import com.pureut.production.domain.vo.LineStockVo;
import com.pureut.production.domain.vo.UpdateLineStockStatusVo;
import com.pureut.production.mapper.LineStockMapper;
import com.pureut.production.service.ILineStockService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 线边库存实现类
 * @Date 23-02-21
 * @Author hl
 **/
@Service
public class LineStockServiceImpl extends ServiceImpl<LineStockMapper, LineStock> implements ILineStockService {

    @Resource
    private LineStockMapper lineStockMapper;

    @Resource
    private FeignService feignService;

    /**
     * 查询线边库存
     */
    @Override
    public List<LineStockDto> selectLineStockList(LineStockVo lineStockVo) {
        List<LineStockDto> lineStockDtos = lineStockMapper.selectLineStockList(lineStockVo);
        for (LineStockDto lineStockDto : lineStockDtos) {
            List<SysDictData> statusArray = DictUtils.getDictCache("wms_stock_info_grade");
            Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
            lineStockDto.setMaterialGrade(statusMap.get(String.valueOf(lineStockDto.getMaterialGrade())));
        }
        return lineStockDtos;
    }

    /**
     * 新增线边库存
     */
    @Override
    public int insertLineStock(LineStock lineStock) {
        return lineStockMapper.insertLineStock(lineStock);
    }

    /**
     * 查询线边汇总
     */
    @Override
    public List<LineStockSummaryDto> selectLineStockSummaryList(LineStockVo lineStockVo) {
        return lineStockMapper.selectLineStockSummaryList(lineStockVo);
    }

    /**
     * 根据物料号码获取线边库存数据（fegin调用）
     *
     * @param materialNumber
     * @return
     */
    @Override
    public LineStockDto getLineStockData(String materialNumber) {
        return lineStockMapper.getLineStockData(materialNumber);
    }

    /**
     * 查询线边仓数据（冻结列表查询 feign调用）
     *
     * @param lineStockVo
     * @return
     */
    @Override
    public List<LineStockDto> getLineStockList(LineStockVo lineStockVo) {
        if (lineStockVo.getEndTime() == null && lineStockVo.getStartTime() == null && lineStockVo.getLineCode() == null && lineStockVo.getStockAlCode() == null && lineStockVo.getMaterialCode() == null && lineStockVo.getMaterialNumber() == null) {
            return new ArrayList<>();
        } else {
            return lineStockMapper.getLineStockList(lineStockVo);
        }
    }

    /**
     * 批量修改线边仓数据状态 feign调用
     *
     * @param updateLineStockStatusVo
     * @return
     */
    @Override
    public boolean updateLineStockStatus(UpdateLineStockStatusVo updateLineStockStatusVo) {
//        List<Long> ids = updateLineStockStatusVo.getIds();
        List<String> materialNumberList = updateLineStockStatusVo.getMaterialNumberList();
        List<LineStock> lineStocks = lineStockMapper.selectList(new QueryWrapper<LineStock>().lambda().in(LineStock::getMaterialNumber, materialNumberList));
        List<Long> ids = new ArrayList<>();
        for (LineStock entity : lineStocks) {
            ids.add(entity.getStockId());
//            if (updateLineStockStatusVo.getFreeValue() != 0) {
//                entity.setFreezeMark(updateLineStockStatusVo.getFreeValue());
//            }
//            if (updateLineStockStatusVo.getMaterialGradeValue() != 0) {
//                entity.setMaterialGrade(updateLineStockStatusVo.getMaterialGradeValue());
//            }
        }
        int freeValue = updateLineStockStatusVo.getFreeValue();
        int materialGradeValue = updateLineStockStatusVo.getMaterialGradeValue();
        return lineStockMapper.updateDataByIds(ids, freeValue, materialGradeValue);
    }

    /**
     * 通过物料号码修改线边仓物料冻结状态
     *
     * @param materialNumber
     */
    @Override
    public void updateStatusThaw(String materialNumber) {
        LineStock lineStocks = lineStockMapper.selectOne(new QueryWrapper<LineStock>().lambda().eq(LineStock::getMaterialNumber, materialNumber));
        if (lineStocks != null) {
            lineStocks.setFreezeMark(1);
            updateById(lineStocks);
        }
    }

    /**
     * 新增线边仓数据
     *
     * @param lineStock
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addLineStock(LineStock lineStock) {

        Long storage = feignService.getStorage(lineStock.getLineCode());

        if (storage==null){
            throw new GlobalException("服务调用失败");
        }

        return save(lineStock);
    }

    /**
     * 线边库存批量新增
     *
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addBatchLineStock(List<LineStock> list) {
        Long storage = feignService.getStorage(list.get(0).getLineCode());

        if (storage==null){
            throw new GlobalException("服务调用失败");
        }

        for (LineStock lineStock : list) {
            lineStock.setWareId(storage);
        }

        return saveBatch(list);
    }

    /**
     * 删除线边仓库存
     *
     * @param materialNumber
     * @return
     */
    @Override
    public boolean deleteLineStock(String materialNumber) {
        return lineStockMapper.deleteByMaterialNumber(materialNumber) > 0;
    }

    @Override
    public boolean deleteBatchLineStock(String wareAlCode) {
        return lineStockMapper.deleteByWareAlCode(wareAlCode);
    }


}
